package com.e3.purchase.po;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.corp.client.CorpRefDialog;
import com.e3.basdoc.cust.client.CustRefDialog;
import com.e3.basdoc.dept.client.DeptRefDialog;
import com.e3.bill.client.BillHelper;
import com.e3.purchase.model.PODetailModel;
import com.e3.purchase.model.POMainModel;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class POHelper extends BillHelper{
	
	public static POHelper INSTANCE = new POHelper();
	
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFormFieldDisplayName(name);

		if(name.equals(POMainModel.PURCHASE_CORP)||name.equals(POMainModel.RECEIVER_CORP)){
			formField = MyFormFieldFactory.createRefField(name, dispName, new CorpRefDialog(), true);
		}
		else if(name.equals(POMainModel.PURCHASE_DEPT)){
			formField = MyFormFieldFactory.createRefField(name, dispName, new DeptRefDialog(), false);
		}
		else if(name.equals(POMainModel.INVOICE_CLIENT)){
			formField = MyFormFieldFactory.createStringField(name, dispName, 50, false);
		}
		else if(name.equals(POMainModel.PURCHASER)){
			formField = MyFormFieldFactory.createStringField(name, dispName, 50, false);
		}
		else if(name.equals(POMainModel.PAYMENT_PROTOCAL)){
			formField = MyFormFieldFactory.createStringField(name, dispName, 50, false);
		}
		else if(name.equals(POMainModel.SUPPLIER)){
			formField = MyFormFieldFactory.createRefField(name, dispName, new CustRefDialog(), false);
		}
		else{
			return BillHelper.INSTANCE.createFormField(name);
		}
		return formField;
	}

	public List<ColumnConfig> getMainGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = POMainModel.BILLNO;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = POMainModel.PURCHASE_CORP;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = POMainModel.PURCHASE_DEPT;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = POMainModel.PURCHASER;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = POMainModel.RECEIVER_CORP;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = POMainModel.SUPPLIER;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = POMainModel.INVOICE_CLIENT;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = POMainModel.PAYMENT_PROTOCAL;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = POMainModel.STATUS;
	    column.setId(name);   
	    column.setHeader(getMainGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    return configs;
	}
	public List<ColumnConfig> getDetailGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = PODetailModel.INVENTORY;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = PODetailModel.MEASUREUNIT;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = PODetailModel.NUMBER;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = PODetailModel.PRICE;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.EXTAXPRICE;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.TAXPRICE;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.AMOUNT;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.TAX;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.TAX_AMOUNT;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.TOTAL_AMOUNT;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.WAREHOUSE;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.STATUS;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.PLAN_ARRTIME;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = PODetailModel.ARRTIME;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = PODetailModel.APPLY_DEPT;
	    column.setId(name);   
	    column.setHeader(getDetailGridFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column); 
	    
	    return configs;
	}
	
	public List<Field<?>> getFormFields(String[] fNames){
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name:fNames) {
			Field<?> field = createFormField(name);
			fields.add(field);
		}
		return fields;
	}
	
	public String getDetailGridFieldDisplayName(String code) {
		return PODetailFieldsConstant.INSTANCE.getString(code);
	}
	
	public String getMainGridFieldDisplayName(String code) {
		return POMainFieldsConstant.INSTANCE.getString(code);
	}
	
	public String getFormFieldDisplayName(String code) {
		return POMainFieldsConstant.INSTANCE.getString(code);
	}
	
	protected boolean isNotNullField(String name) {
		return false;
	}
}
